package com.example.treehole.ui.page.postDetail

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.treehole.ui.component.formatTime
import com.example.treehole.ui.model.CommentToPost
import com.example.treehole.ui.model.PostDetail
import com.example.treehole.ui.theme.postCyan
import java.util.Locale

@Composable
fun ContentCard(postDetail: PostDetail) {
    val formatPostId = String.format(Locale.PRC, "#%08d", postDetail.postId)
    val timeText = formatTime(postDetail.createDate)
    val commentCount = postDetail.commentCount

    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            .background(postCyan)
    ) {
        item {
            Text(
                text = postDetail.title,
                style = MaterialTheme.typography.headlineSmall,
                modifier = Modifier.padding(horizontal = 16.dp).padding( top = 8.dp)
            )
        }
        item {
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = postDetail.authorName + if (postDetail.commentName == postDetail.authorName) "(我)" else "",
                    modifier = Modifier.padding(start = 16.dp)
                )
                Spacer(modifier = Modifier.width(8.dp))
                Text(
                    text = formatPostId,
                    color = Color.Gray,
                    modifier = Modifier.padding(end = 16.dp)
                )
            }
        }
        item {
            HorizontalDivider(
                modifier = Modifier.padding(vertical = 8.dp),
                color = Color.Gray,
                thickness = 1.dp
            )
        }
        item {
            Text(
                text = postDetail.content,
                style = MaterialTheme.typography.bodyLarge,
                modifier = Modifier.padding(vertical = 8.dp, horizontal = 16.dp)
            )
        }
        item {
            Text(
                text = timeText,
                modifier = Modifier.padding(start = 16.dp),
                color = Color.Gray
            )
        }
        item {
            HorizontalDivider(
                modifier = Modifier.padding(vertical = 8.dp),
                color = Color.Gray,
                thickness = 1.dp
            )
        }
        item {
            Text(
                text = "评论列表，共${commentCount}条评论",
                modifier = Modifier.padding(start = 16.dp),
                color = Color.Gray
            )
        }
        item {
            HorizontalDivider(
                modifier = Modifier.padding(vertical = 8.dp),
                color = Color.Gray,
                thickness = 1.dp
            )
        }

        items(postDetail.cmtList) { item: CommentToPost ->
            PostCommentCard(
                item,
                postDetail.commentName
            )
        }
    }

}
